<?php
namespace Sys\Controller;
use Common\Controller\BaseController;
class ExamController extends BaseController{
	public function __construct(){
		parent::__construct();
	}
	public function index(){
		$this-> display();
	}

	public function exam(){
			$a = M('exam_items')->field('id,exam_name')-> where(['up' => '0'])-> select();
			$term = M('dict')->field('val')-> where(['field' => 'term'])-> select();
			$this-> assign('b',$a);
			$this-> assign('term',$term);	
			$this-> getData();
			$this-> display();
	}
     
	private function getData(){
			$student = M('students')->field('sname,student_id')-> where(['term_id'=>$_POST['term_id']])-> select();
			$item = M('exam_items')->field('id,exam_name')-> where(['up'=>$_POST['up']])-> select();
			$this-> assign('student',$student);	
			$this-> assign('item',$item);
	}

	public function addExam(){
		M('exams')-> create();
		if(I('post.exam_id')){
			M('exams')-> save();	
		}else{
			$id = M('exams')-> add();
			$this-> ajaxReturn($id);			
		}

	}

	public function addExamItem(){
		$option = M('exam_items')-> where(['up'=>'0'])-> select();
		$res = M('exam_items')
				->field('pj_exam_items.exam_name as e1,s1.exam_name as e2')
				->join('pj_exam_items s1 on s1.up = pj_exam_items.id')
				->select();
		$this-> assign('res',$res);
		$this-> assign('option',$option);
		$this-> display();
	}

	public function doAddExamItem(){
		M('exam_items')-> create();
		M('exam_items')-> add();
		$this-> success('添加成功',U('addExamItem'));
	}

	
	public function doAddExam(){
		//dump($_POST);
		foreach($_POST['val'] as $v){
		$content=json_decode($v,true);
		$where=[
                'student_id'=>$content[student_id],
                'type_dict_id'=>$content[type_dict_id],
            ];
            if(M('exams')->where($where)->find()){
                $res='学生成绩已登入，请勿重复添加';
                $this->error($res);
            }
		M('exams')-> add($content);
		//dump($content);
			
		
		}
	$this->success('success');
	}
		 public function showscore(){
			 //dump(session());
			 //dump($_POST);
		if(session('?id')){
			$where = [
				'student_id' => session(id),
			];	
		}else{
			$this-> error('只可学生登录','index');
		}
		if(I('post.up')){
			$item = M('exam_items')->field('id,exam_name')-> where(['up'=>I('post.up')])-> select();
		$sql = 'SELECT IFNULL(pj_students.sname,\'studentN\') as `name`, ';
		foreach($item as $k => $v){

			$sql .= ' sum(if(type_dict_id="'.$v['id'].'",score,0)) as "'.$v['exam_name'].'",';
		}
		$sql = substr($sql,0,-1);
				echo '<br>';

		$sql .='
					from 
						`pj_exams` INNER JOIN pj_students on pj_students.student_id=pj_exams.student_id 
						WHERE
				pj_students.student_id = '.session(id).' GROUP BY `name`';
					
					
		$M = new \Think\Model();
		$res = $M->query($sql);
		
			$this->assign('a',$item);
			//dump($item);
		}
		$examType = M('exam_items')->field('id,exam_name')-> where(['up' => '0'])-> select();
		$article=M('exam_items')->field('exam_name')->where(['up'=>['NEQ','0']])->select();
		$sname=M('students')->field('sname')->select();
		$score=M('exams')->field('score')->select();
		
		//dump($score);
		$this->assign(article,$article);
		$this-> assign('examType',$examType);
		$this->assign(sname,$sname);
		$this->assign(score,$res);
		$this->display();
	}
	public function editExam(){}
	public function doEditExam(){}
	public function doDelExam(){}
}